<?php
namespace Home\Controller;

class SubOrderController extends BaseController{
	private $pay = array(1=>'货到付款',2=>'支付宝',4=>'财付通',6=>'微信支付',8=>'云支付');
	
    public function index(){
    	if (IS_POST){
    		$Order = D('Admin/Order');
    		$object = $Order->insert(1);
    		if (is_array($object)){
    			$this->sendmail($object);
    			if (I('post.pay') == 2){
					$this->success(U('Pay/alipay',array('oid'=>$object['oid'])));
    			}elseif (I('post.pay') == 4){
					$this->success(U('Pay/tenpay',array('oid'=>$object['oid'])));
    			}elseif (I('post.pay') == 6){
					$this->success(U('Pay/wxpay',array('oid'=>$object['oid'])));
    			}elseif (I('post.pay') == 8){
					$this->success(U('Pay/yunpay',array('oid'=>$object['oid'])));
    			}
    			$this->success(NULL,$object['success'],0,2);
    		}else{
    			$this->failed($object);
    		}
    	}
    }
    
    private function sendmail($object){
    	$Smtp = D('Admin/Smtp');
    	if ($Smtp->total() > 0){
    		$object2 = $Smtp->one2();
    		if ($object2){
    			try{
    				import('Yvjie.PHPMailer');
    				$mail = new \PHPMailer();
    				$mail->CharSet = 'UTF-8';
    				$mail->IsSMTP();
    				$mail->SMTPAuth = true;
    				$mail->Port = $object2[0]['port'];
    				$mail->Host = $object2[0]['smtp'];
    				$mail->Username = $object2[0]['user'];
    				$mail->Password = $object2[0]['pass'];
    				$mail->From = $object2[0]['email'];
    				$mail->FromName = $object2[0]['email'];
    				foreach (explode(',',C('ADMIN_MAIL')) as $value){
    					$mail->AddAddress($value,$mail->FromName);
    				}
    				$mail->IsHTML(true);
    				$mail->Subject = $this->mail(C('ORDER_MAIL_SUBJECT'),$object);
    				$mail->Body = $this->mail(C('ORDER_MAIL_CONTENT'),$object);
    				$mail->Send();
    			}catch (phpmailerException $e){
    				echo $e->errorMessage();
    			}
    		}
    	}
    }
    
    private function mail($content,$object){
    	import('Yvjie.QQWry');
    	$QQWry = \QQWry::getInstance();
    	
    	$Product = D('Admin/Product');
    	$object2 = $Product->one($object['pid']);
		
    	return str_replace(array(
    		'{oid}',
    		'{proname}',
    		'{proprice}',
    		'{procount}',
    		'{prototal}',
    		'{name}',
    		'{tel}',
    		'{province}',
    		'{city}',
    		'{county}',
    		'{address}',
    		'{post}',
    		'{note}',
    		'{email}',
    		'{ip}',
    		'{referrer}',
    		'{pay}'
    	),array(
    		$object['oid'],
    		$object2 ? $object2['name'] : '',
    		$object['price'],
    		$object['count'],
    		$object ? number_format($object['price']*$object['count'],2,'.','') : '0.00',
    		$object['name'],
    		$object['tel'],
    		$object['province'],
    		$object['city'],
    		$object['county'],
    		$object['address'],
    		$object['post'],
    		$object['note'],
    		$object['email'],
    		$object['ip'].' '.$QQWry->getAddr($object['ip']),
    		$object['referrer'] ? '<a href="'.$object['referrer'].'" target="_blank">'.$object['referrer'].'</a>' : '直接进入',
    		$this->pay[$object['pay']]
    	),$content);
    }
}